//
//  MBProgressHUD+CT.m
//
//  Created by tao.chen on 14-12-5.
//  Copyright (c) 2014年 PayEigs. All rights reserved.
//

#import "MBProgressHUD+CT.h"

@implementation MBProgressHUD (CT)
// 1.5秒之后再消失
#define ShowTime 1.5


#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    [self show:text icon:icon view:view completionBlock:nil];
}

+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view  completionBlock:(void(^)())completionBlock
{
    if (view == nil)
    {
         view = [UIApplication sharedApplication].keyWindow;
        
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:ShowTime];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ShowTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD resignLock];
        if(completionBlock)
        {
            completionBlock();
        }
    });
    
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showError:(NSString *)error completionBlock:(void(^)())completionBlock
{
    [self show:error icon:@"error.png" view:nil completionBlock:completionBlock];
}


#pragma mark 显示成功信息
+ (void)showSuccess:(NSString *)success lock:(BOOL)lock
{
    [self showSuccess:success toView:nil lock:lock];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view lock:(BOOL)lock
{
    if (lock) {
        [MBProgressHUD showWithLock];
    }
    [self show:success icon:@"success.png" view:view];
}


+ (void)showSuccess:(NSString *)success  lock:(BOOL)lock completionBlock:(void(^)())completionBlock
{
    if (lock) {
        [MBProgressHUD showWithLock];
    }
    [self show:success icon:@"success.png" view:nil completionBlock:completionBlock];
}




#pragma mark 显示等待
+ (MBProgressHUD *)showWaiting:(NSString *)message toView:(UIView *)view lock:(BOOL)lock
{
    if (lock) {
        [MBProgressHUD showWithLock];
    }
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    return hud;
}

+ (MBProgressHUD *)showWaiting:(NSString *)message lock:(BOOL)lock
{
    return [self showWaiting:message toView:nil lock:lock];
}


#pragma mark 显示提示信息，位于中间
+(void)showMessage:(NSString *)message toView:(UIView *)view lock:(BOOL)lock
{
    if (lock) {
        [MBProgressHUD showWithLock];
    }else{
        [MBProgressHUD resignLock];
    }
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.labelText = message;
    hud.margin = 20.f;
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hide:YES afterDelay:ShowTime];
}
+(void)showMessage:(NSString *)message lock:(BOOL)lock
{
    [self showMessage:message toView:nil lock:lock];
}
+ (void)showMessage:(NSString *)message lock:(BOOL)lock completionBlock:(void(^)())completionBlock
{
    [self showMessage:message toView:nil lock:lock];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ShowTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD resignLock];
        if(completionBlock)
        {
            completionBlock();
        }
    });
}

+(void)showWithLock
{
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled=NO;
}
+(void)resignLock
{
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled=YES;
}

+ (void)hideHUDForView:(UIView *)view
{
    [MBProgressHUD resignLock];
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
@end
